import axios from 'axios';

const service = axios.create({
	// process.env.NODE_ENV === 'development' 来判断是否开发环境
	// easy-mock服务挂了，暂时不使用了
	// baseURL: 'https://www.easy-mock.com/mock/592501a391470c0ac1fab128',
	baseURL: 'https://cache.iminutes.cn/api',
	timeout: 5000
});

service.interceptors.request.use(
	config => {
		let token = sessionStorage.getItem('token')
		//后面接口需要携带token的都添加到这个位置
		config.headers.Authorization = "Bearer " + token
		return config;
	},
	error => {
		console.log(error);
		return Promise.reject();
	}
);
//导入路由
import router from '../router/index'
service.interceptors.response.use(
	response => {
		if (response.data.status == 401) {
			router.push('/login')
		}
		if (response.status === 200) {
			return response.data;
		} else {
			Promise.reject();
		}
	},
	error => {
		console.log(error);
		return Promise.reject();
	}
);

export default service;
